package com.marvels.common.utils;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


public class BeanUtils {
	private BeanUtils(){
		//do nothing
	}
	/**
	 * JAVABEAN对象转换成Map对象
	 * @param bean
	 * @return
	 */
	public static Map<String,Object> beanConverToMap(Object bean){
		
		Map<String, Object> map = new HashMap<String, Object>();   
		
		 if(bean == null)  
	            return map;      
	  
        try {
        	BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());    
        	PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();    
        	for (PropertyDescriptor property : propertyDescriptors) {    
        		String key = property.getName();    
        		if (key.compareToIgnoreCase("class") == 0) {   
        			continue;  
        		}  
        		Method getter = property.getReadMethod();  
        		Object value = getter!=null ? getter.invoke(bean) : null;  
        		map.put(key, value);  
        	}    
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("将javaBean转换成Map异常！",e);
			throw new ClassCastException("javaBean转换成Map异常");
		}
  
        return map;  
	}
	
	
	/**
     * 实体对象转成Map
     *
     * @param obj 实体对象
     * @return
     */
    public static Map<String, Object> object2Map(Map<String, Object> map,Object obj) {
        if (obj == null) {
            return map;
        }
        Class clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                if(!field.getName().equals("serialVersionUID")){
                	Object object=field.get(obj);
                	 map.put(field.getName(),org.springframework.util.StringUtils.isEmpty(object)?"":object);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
    
    
    /**
     * 实体对象转成Map
     * @param map
     * @param obj
     * @return
     */
    public static Map<String, String> objectToMap(Map<String, String> map,Object obj) {
        if (obj == null) {
            return map;
        }
        Class clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                if(!field.getName().equals("serialVersionUID")){
                	String object=(String) field.get(obj);
                	 map.put(field.getName(),org.springframework.util.StringUtils.isEmpty(object)?"":object);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
    
	/**
	 * json转map
	 * @param map
	 * @param jsonStr
	 * @return
	 */
	public static Map<String, Object> parseJSON2Map(Map<String, Object> map,String jsonStr){    
        JSONObject json = JSONObject.fromObject(jsonStr);    
        for(Object k : json.keySet()){    
            Object v = json.get(k);     
            if(v instanceof JSONArray){    
                List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();    
                Iterator<JSONObject> it = ((JSONArray)v).iterator();    
                while(it.hasNext()){    
                    JSONObject json2 = it.next();    
                    list.add(parseJSON2Map(map,json2.toString()));    
                }    
                map.put(k.toString(), list);    
            } else if(v instanceof JSONObject){    
            	parseJSON2Map(map,v.toString());
            } else{
            	map.put(k.toString(), org.springframework.util.StringUtils.isEmpty(v)?"":v);  
            }
        }    
        return map;    
    } 
}
